/*
 * Comanda.java
 *
 * Created on 03/04/2013, 17:34:15
 */
package forms;

import classes.Conexao;
import classes.Utilitarios;
import javax.swing.JOptionPane;


/**
 *
 * @author Yanni
 */
public class FormCadastrarComanda extends javax.swing.JInternalFrame {

    Utilitarios u = new Utilitarios();
    private String textoPermitido = "0123456789";
    
    /** Creates new form Comanda */
    public FormCadastrarComanda() {
        initComponents();   
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        ativaButtonGroup = new javax.swing.ButtonGroup();
        lblCdComanda = new javax.swing.JLabel();
        txtCdComanda = new javax.swing.JTextField(new classes.CaracteresPermitidos(textoPermitido), "", 20);
        ativaPanel = new javax.swing.JPanel();
        rbtNao = new javax.swing.JRadioButton();
        rbtSim = new javax.swing.JRadioButton();
        btnCadastrarComanda = new javax.swing.JButton();
        btnLimpar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Cadastrar Comanda");
        setName("frmComanda"); // NOI18N

        lblCdComanda.setText("Código:");
        lblCdComanda.setToolTipText("");

        txtCdComanda.setToolTipText("Digite o código da comanda");

        ativaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Ativa?"));
        ativaPanel.setToolTipText("Comanda ativa ou inativa?");

        ativaButtonGroup.add(rbtNao);
        rbtNao.setText("Não");
        rbtNao.setToolTipText("Selecione esta opção se a comanda não estiver ativa");

        ativaButtonGroup.add(rbtSim);
        rbtSim.setSelected(true);
        rbtSim.setText("Sim");
        rbtSim.setToolTipText("Selecione esta opção se a comanda estiver ativa");

        javax.swing.GroupLayout ativaPanelLayout = new javax.swing.GroupLayout(ativaPanel);
        ativaPanel.setLayout(ativaPanelLayout);
        ativaPanelLayout.setHorizontalGroup(
            ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ativaPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtNao)
                    .addComponent(rbtSim))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        ativaPanelLayout.setVerticalGroup(
            ativaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ativaPanelLayout.createSequentialGroup()
                .addComponent(rbtSim)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbtNao))
        );

        btnCadastrarComanda.setText("Cadastrar");
        btnCadastrarComanda.setToolTipText("Clique aqui para cadastrar a comanda");
        btnCadastrarComanda.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnCadastrarComandaActionPerformed(evt);
            }
        });

        btnLimpar.setText("Limpar");
        btnLimpar.setToolTipText("Clique aqui para limpar os valores");
        btnLimpar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnLimparActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCadastrarComanda)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpar, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ativaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCdComanda)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCdComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 42, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCdComanda)
                    .addComponent(txtCdComanda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ativaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCadastrarComanda)
                    .addComponent(btnLimpar))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-209)/2, (screenSize.height-229)/2, 209, 229);
    }// </editor-fold>//GEN-END:initComponents

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        u.limparTextFields(this);
        rbtSim.setSelected(true);
    }//GEN-LAST:event_btnLimparActionPerformed

    private void btnCadastrarComandaActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCadastrarComandaActionPerformed
    {//GEN-HEADEREND:event_btnCadastrarComandaActionPerformed
        int cod;
        char ativa = ' ';        
        
        try
        {
            cod = Integer.parseInt(txtCdComanda.getText());
            if(rbtSim.isSelected()) {
                ativa = 's';
            }
            else if(rbtNao.isSelected()) {
                ativa = 'n';
            }
            
            String sql = "INSERT INTO COMANDA VALUES(" + cod + ", UPPER('" + ativa + "'))";
                        
            if(Conexao.atualizar(sql) == -1)
            {
                JOptionPane.showMessageDialog(null, "O registro não pode ser inserido:\n" + Conexao.getErro(), "Cadastro", 0);
                btnLimparActionPerformed(evt);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Cadastrado com sucesso.", "Cadastro", 1);
                btnLimparActionPerformed(evt);  
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, "Erro: \n" + e.getMessage(), "Erro!", 0);
            u.limparTextFields(this);
        }
    }//GEN-LAST:event_btnCadastrarComandaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup ativaButtonGroup;
    private javax.swing.JPanel ativaPanel;
    private javax.swing.JButton btnCadastrarComanda;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JLabel lblCdComanda;
    private javax.swing.JRadioButton rbtNao;
    private javax.swing.JRadioButton rbtSim;
    private javax.swing.JTextField txtCdComanda;
    // End of variables declaration//GEN-END:variables
}
